package no27;

import javax.swing.tree.TreeNode;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/5/17 14:21
 * 二叉树的镜像
 * https://leetcode.cn/problems/er-cha-shu-de-jing-xiang-lcof/
 */
public class Solution {

    // 方法一: 改变原来的树
    public TreeNode mirrorTree01(TreeNode root) {
        if (root == null) {
            return null;
        }
        // 交换左右子树
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        // 继续递归交换
        mirrorTree01(root.left);
        mirrorTree01(root.right);
        return root;
    }

    // 方法二: 创建一个新二叉树
    public TreeNode mirrorTree02(TreeNode root) {
        if(root == null){
            return null;
        }
        TreeNode newRoot = new TreeNode(root.val);
        newRoot.left = mirrorTree02(root.right);
        newRoot.right = mirrorTree02(root.left);
        return newRoot;
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
